<template>
  <div>
    <router-link 
      to="/" 
      tag="div" 
      class="header-abs"
      v-show="showAbs">
      <div class="header-abs-back iconfont">&#xe624;</div>
    </router-link>
    <div 
      class="header-fixed" 
      v-show="!showAbs"
      :style="opacityStyle">
      <router-link to="/">
        <div class="iconfont back-fixed-icon">&#xe624;</div>
      </router-link>
      景点详情
    </div>
  </div>
</template>

<script>
export default {
  name: 'DetailHeader',
  data () {
    return {
      showAbs: true,
      opacityStyle: {
        opacity: 0
      }
    }
  },
  methods: {
    handleScroll () {
      const top = document.documentElement.scrollTop //要获取当前页面的滚动条纵坐标位置
      if (top > 60) {
        let opacity = top / 160
        opacity = opacity > 1 ? 1 : opacity
        this.opacityStyle = {opacity}
        this.showAbs = false
      } else {
        this.showAbs = true
      }
    }
  },
  activated () { //因为使用了keep-alive 
    window.addEventListener('scroll', this.handleScroll)//对全局事件的绑定，会影响其他页面
  },
  deactivated () {//使用了keep-alive ,对应也有deactivated,对全局事件的解绑，使得不影响其他页面
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/variables.styl'
  .header-abs
    position : absolute
    left : .1rem
    top: .1rem
    width : .72rem
    height : .72rem
    line-height : .72rem
    text-align : center
    border-radius : .4rem
    background : rgba(0, 0, 0, .5)
    .header-abs-back
      color: #fff
      font-size : .4rem
  .header-fixed
    z-index : 22
    position : fixed //这里一定不能是absolute，否则会随着父组件移动，不能展示在界面上
    left : 0
    top: 0
    right : 0  
    width : 100% 
    overflow: hidden
    height: $headerHeight
    line-height : $headerHeight
    color: #fff
    background: $bgColor
    font-size : .32rem
    text-align : center
    .back-fixed-icon
      position : absolute
      top: 0
      left : 0
      width : .64rem
      color: #fff
</style>